<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="菜单名称">
          <el-input v-model="searchForm.name" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="add-button">添加</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row
        row-key="uid" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column type="index" align="center" label="序号" width=50px class-name="table-index"></el-table-column>
        <el-table-column prop="name" align="left" label="菜单名称"></el-table-column>
        <el-table-column prop="path" align="center" label="路由地址"></el-table-column>
        <el-table-column prop="component" align="center" label="组件路径"></el-table-column>
        <el-table-column prop="sort" align="center" label="菜单排序"></el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="菜单名称">
          <el-input v-model.trim="form.name" placeholder="菜单名称"></el-input>
        </el-form-item>
        <el-form-item label="路由地址">
          <el-input v-model.trim="form.path" placeholder="路由地址"></el-input>
        </el-form-item>
        <el-form-item label="组件路径">
          <el-input v-model.trim="form.component" placeholder="组件路径"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序">
          <el-input v-model.trim="form.sort" placeholder="菜单排序"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-cascader ref="positionInfoCascader" v-model="form.pid" style="width: 100%;" :options="list"
            :props="positionInfoProps" separator="-" clearable>
          </el-cascader>
        </el-form-item>
        <div style="margin: 10px 120px;">
          一级菜单不选上级菜单
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as menu from '@/api/menu'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data () {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        // pageIndex: 1,
        // pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      dialogTit: '',
      form: {},
      positionInfoProps: {
        value: "uid",
        label: "name",
        children: "children",
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      menu.queryByPage(param).then(response => {
        if (response.code == 200 && response.data.rows) {
          let arr = response.data.rows
          this.list = this.buildTree(arr)
          this.list.map(item => {
            return item.children.sort((a, b) => a.sort - b.sort);
          })
          //this.total = response.data.total
        }
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })
    },
    buildTree (data) {
      let hashTable = Object.create(null);
      data.forEach(aData => hashTable[aData.uid] = { ...aData, children: [] });
      let tree = [];
      data.forEach(aData => {
        if (aData.pid) hashTable[aData.pid].children.push(hashTable[aData.uid]);
        else tree.push(hashTable[aData.uid])
      });
      return tree.sort((a, b) => a.sort - b.sort);
    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd () {
      this.dialogTit = '新增'
      this.form = {}
      this.dialogVisible = true
    },
    showEdit (row) {
      this.dialogTit = '编辑'
      this.form = Object.assign({}, row)
      delete this.form.createTime
      delete this.form.updateTime
      this.dialogVisible = true
    },
    submit () {
      if (this.dialogTit === '新增') {
        this.creatHandle()
      } else if (this.dialogTit === '编辑') {
        this.editHandle()
      }
    },
    creatHandle () {
      menu.insert(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle () {
      menu.update(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        menu.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    }
  }
}
</script>
